/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package helpdiabetesj2me;

import java.util.Date;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author Johan VM Windows 7
 */
public class ShowActiveInsulin extends MyCanvas implements CommandListener {

    private final Command Terug, Wijzig;
    static final String[] str_terug = {"Terug"};
    static final String[] str_active_insulin = {"Totaal Actieve Insuline = "};
    static final String[] str_list_of_bolusses = {"Lijst van Actieve Bolussen : "};
    static final String[] str_wijzig = {"Wijzig"};
    
    int offSetForRecords;
    int tempid;
    int shownrecid;
    int bottomYOfShownRecord = 0;
    ActiveInsulinEvent[] activeInsulinEvents;
    static public boolean activeInsulinEventsChanged = false;//will be used by addInsulinEvent, to trigger refresh
     
    String GetClassName() {
        return("ShowActiveInsulin");
    }

    public ShowActiveInsulin(Font userfont, HelpDiabetes midlet) {
        super(userfont,midlet);
        Terug = new Command(str_terug[HelpDiabetes.languageIndex],str_terug[HelpDiabetes.languageIndex],Command.EXIT,1);
        this.addCommand(Terug);
        Wijzig = new Command(str_wijzig[HelpDiabetes.languageIndex],Command.OK,2);
        this.addCommand(Wijzig);

        this.setCommandListener(this);
        curdisp = midlet.getDisplay().getCurrent();
        midlet.getDisplay().setCurrent(this);
        activeInsulinEvents = ActiveInsulinManagement.getListOfActiveInsulinEvents();
        shownrecid = 1;
    }

    
    protected void paint(Graphics graphics) {
        x=0;y=0;
        graphics.setFont(userfont);

        graphics.setColor(255,255,255);
        graphics.fillRect(0, 0, scrwidth, scrhght);
        paintstring(str_active_insulin[HelpDiabetes.languageIndex].getBytes(),false,graphics,userfont);
        
        //calculate active insulin
        float amountToShow = ActiveInsulinManagement.getTotalActiveInsulin();
        float multiplywith10 = amountToShow * (float) 10;
        float Plus0Dot5 = multiplywith10 + (float) 0.5;
        int toInt = (int)Plus0Dot5;
        float DivideBy10 = ((float)toInt) / (float)10;
        paintstring((new java.lang.Float(DivideBy10)).toString().getBytes(),false,graphics,userfont);
        
        //a horizontal line
        x=0;y= y+fonthght;
        graphics.drawLine(0,y,scrwidth,y);
        y=y+fonthght/2;
        
        //static text
        paintstring(str_list_of_bolusses[HelpDiabetes.languageIndex].getBytes(),false,graphics,userfont);
        
        //a horizontal line
        x = 0;y = y + fonthght;
        graphics.drawLine(0,y,scrwidth,y);
        y = y + 1;
        
        //list of bolusses
        graphics.setClip(0,y,scrwidth,scrhght - y);
        y = y - offSetForRecords;
       
        if (activeInsulinEventsChanged) {
            activeInsulinEvents = ActiveInsulinManagement.getListOfActiveInsulinEvents();
            activeInsulinEventsChanged = false;
        }
        tempid = 0;//activeInsulinEvents.length - 1;
        while (tempid < activeInsulinEvents.length & (y < scrhght)) {
                if (tempid == shownrecid - 1) {
                    if (y < graphics.getClipY()) {
                        offSetForRecords = offSetForRecords - fonthght;
                        repaint();
                    } else {
                        bottomYOfShownRecord = y + drawitem(true, activeInsulinEvents[tempid], graphics) * fonthght;
                        //this is to make the line inverted for the whole width of the screen
                        graphics.setColor(0,0,0);
                        graphics.fillRect(x,y,scrwidth-x,fonthght);
                        graphics.setColor(255,255,255);

                        //bottom of currently shown record = y
                        //bottomYOfShownRecord = y + fonthght;
                        if (bottomYOfShownRecord > scrhght) {
                            offSetForRecords = offSetForRecords  + fonthght;
                            repaint();
                        }
                    }
                } else {
                    drawitem(false, activeInsulinEvents[tempid], graphics);
                }
                x=0;y=y+fonthght;
                tempid++;
        }
        graphics.setClip(0,0,scrwidth,scrhght);
    }

    public void commandAction(Command command, Displayable d) {
        if (command == Terug) {
            midlet.getDisplay().setCurrent(curdisp);
        } else if (command == Wijzig) {
            changeSelectedItem();
        }
    }
    
    private void changeSelectedItem() {
        new AddInsulinEvent(userfont, midlet, null, activeInsulinEvents[shownrecid - 1].getRecordStoreId());
    }
    
    public synchronized void keyPressed (int keyCode) {    
        switch(getGameAction(keyCode)) {
            case LEFT:
                midlet.getDisplay().setCurrent(curdisp);
                break;
            case RIGHT:
                new AddInsulinEvent(userfont, midlet, null, -1);
                break;
            case UP:
                if (shownrecid > 1)
                    shownrecid--;
                repaint();
                break;
            case DOWN:
                if (shownrecid < activeInsulinEvents.length)
                    shownrecid++;
                break;
            case FIRE:
                changeSelectedItem();
                break;
        }
    }
    
    /**
     * 
     * @return number of lines needed to draw the item
     */
    private  int drawitem(boolean invert,ActiveInsulinEvent activeInsulinEvent,Graphics graphics) {
        String toPrint = Utilities.getHHmmAsString(activeInsulinEvent.getDate());
        toPrint += "  ";
        toPrint += activeInsulinEvent.getAmount();
        if (activeInsulinEvent.getDurationInMinutes() > 0) {
            float durationInHours = ((float)((int)((((float)activeInsulinEvent.getDurationInMinutes() / 60) * 10) + 0.5)))/10;
            toPrint += " (" + durationInHours + ") ";
        }
        toPrint += " ==> ";
        
        float amountToShow = ActiveInsulinManagement.getActiveInsulin(
                (int)((new Date()).getTime() - activeInsulinEvent.getDate().getTime())/1000
                , 
                activeInsulinEvent.getAmount()
                ,
                activeInsulinEvent.getDurationInMinutes()
        );
        float multiplywith10 = amountToShow * (float) 10;
        float Plus0Dot5 = multiplywith10 + (float) 0.5;
        int toInt = (int)Plus0Dot5;
        float DivideBy10 = ((float)toInt) / (float)10;

        toPrint += DivideBy10;
        if (activeInsulinEvent.getDurationInMinutes() > 0) {
            if (((new Date()).getTime() - activeInsulinEvent.getDate().getTime())/1000 < activeInsulinEvent.getDurationInMinutes()*60) {
                float remainingAmount = activeInsulinEvent.getAmount() - ((float)(((new Date()).getTime() - activeInsulinEvent.getDate().getTime())/1000))/(activeInsulinEvent.getDurationInMinutes()*60)*activeInsulinEvent.getAmount();
                //int remainingDurationInMinutes = activeInsulinEvent.getDurationInMinutes() - (int)( ((new Date()).getTime() - activeInsulinEvent.getDate().getTime())/1000/60);
                //float remainingDurationInHours = ((float)((int)((((float)remainingDurationInMinutes / 60) * 10) + 0.5)))/10;
                remainingAmount = ((float)((int)(remainingAmount * 10 + 0.5)))/10;
                toPrint += " (" + remainingAmount + ") ";
            }
        }
        
        return paintstring(toPrint.getBytes(), invert, graphics, userfont,true);
    }
}
